package com.ktjiaoyu.travelaiagent.tools;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TextFileTool {
    // 文件根目录
    @Value("${travel.ai.files.root}")
    private String fileRoot;

    @Tool(description = "Read the content of a text file.")
    public String readTextFile(@ToolParam(description = "The name of the text file to be read.", required = true) String fileName,
                               // 注入ToolContext可以获取工具上下文参数
                               ToolContext toolContext) {
        // 建议用会话ID隔离文件，避免读取到其他用户的会话文件
        String chatId = (String) toolContext.getContext().get("chatId");
        log.info("AI调用readTextFile工具读取'{}'文件，chatId: {}", fileName, chatId);
        if(chatId == null){
            chatId = "";
        }else{
            chatId = chatId + "/"; // 拼接路径分割
        }
        // 拼接完整路径
        String filePath = this.fileRoot + "/" + chatId + fileName;
        try {
            // 读取文件内容并返回
            String content = FileUtil.readUtf8String(filePath);
            log.info("AI调用readTextFile工具读取'{}'文件成功", filePath);
            return content;
        }catch (Exception e) {
            log.error("AI调用readTextFile工具读取'{}'文件出现异常: {}", filePath, e.getMessage());
            // 读取文件出错，返回错误信息
            return "An error occurred while reading the file. The error message is: " + e.getMessage();
        }
    }

    @Tool(description = "Write content to a text file.")
    public String writeTextFile(@ToolParam(description = "The name of the text file to be written.") String fileName,
                                @ToolParam(description = "The content to be written into the text file.") String content,
                                // 注入ToolContext可以获取工具上下文参数
                                ToolContext toolContext) {
        // 建议用会话ID隔离文件，避免读取到其他用户的会话文件
        String chatId = (String) toolContext.getContext().get("chatId");
        log.info("AI调用writeTextFile工具写入'{}'文件，chatId: {}", fileName, chatId);
        if(chatId == null){
            chatId = "";
        }else{
            chatId = chatId + "/"; // 拼接路径分割
        }
        // 拼接完整路径
        String filePath = this.fileRoot + "/" + chatId + fileName;
        try {
            // 创建父目录（如果不存在）
            FileUtil.mkParentDirs(filePath);
            // 写入文件内容
            FileUtil.writeString(content, filePath, "utf-8");
            log.info("AI调用writeTextFile工具向'{}'文件写入成功", filePath);
            // 写入成功，返回提示信息
            return "The content has been successfully written to the file : " + fileName;
        }catch (Exception e) {
            log.error("AI调用writeTextFile工具向'{}'文件写入出现异常: {}", filePath, e.getMessage());
            // 写入文件出错，返回错误信息
            return "An error occurred while writing the file. The error message is: " + e.getMessage();
        }
    }
}
